/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vben.ai.controller;

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import com.alibaba.cloud.ai.dashscope.chat.MessageFormat;
import com.vben.ai.constant.ModelConstant;
import com.vben.common.core.util.R;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.Media;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.util.List;

/**
 * @author yuluo
 * @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
 */

@RestController
@RequestMapping("/multi")
public class MultiModelController {

    private final ChatClient dashScopeChatClient;

    private static final String DEFAULT_PROMPT = "详细介绍一下图片的内容是什么？";

    public MultiModelController(ChatModel chatModel) {
        this.dashScopeChatClient = ChatClient.builder(chatModel).build();
    }

    /**
     * 图片分析
     */
    @GetMapping("/imageAnalyze")
    public R<String> image(
            @RequestParam(value = "url") String url,
            @RequestParam(value = "prompt", required = false, defaultValue = DEFAULT_PROMPT) String prompt) throws Exception {

        List<Media> mediaList = List.of(
                new Media(
                        MimeTypeUtils.IMAGE_PNG,
                        new URI(url).toURL()
                )
        );

        UserMessage message = new UserMessage(prompt, mediaList);
        message.getMetadata().put(DashScopeChatModel.MESSAGE_FORMAT, MessageFormat.IMAGE);

        ChatResponse response = dashScopeChatClient.prompt(
                new Prompt(
                        message,
                        DashScopeChatOptions.builder()
                                .withModel(ModelConstant.Qwen_VL_Max_Latest)
                                .withMultiModel(true)
                                .build()
                )
        ).call().chatResponse();

        return R.ok(response.getResult().getOutput().getContent());
    }

}
